﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Xml.Linq;
using AKMII.DMRA.Common;
using AKMII.DMRA.Business.Management;
using AKMII.DMRA.Business.Service;
using System.Threading;
using AKMII.DMRA.Business.Rule;
namespace AKMII.DMRA.Web.AdminPages
{
    public partial class ManageCommonSetting : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            SettingManager sm = new SettingManager();
            CommonSetting commonSetting = sm.GetCommonSetting();
            if (commonSetting != null)
            {
                this.txtAssignBlankZip.Text = commonSetting.MDForAssignBlankZip;
                this.txtCompressDmraFileFolder.Text = commonSetting.FolderForCompressDmraFile;
                this.txtCompressPHouseFileFolder.Text = commonSetting.FolderForCompressPrintingHouseFile;
                this.txtCurrentYear.Text = commonSetting.CurrentYear;
                this.txtDecompressFolder.Text = commonSetting.FolderForDecompress;
                this.txtDecryptFolder.Text = commonSetting.FolderForDecrypt;
                this.txtDMRARecipient.Text = commonSetting.DMRARecipient;
                this.txtDownloadFolder.Text = commonSetting.FolderForDownload;
                this.txtDownloadFtp.Text = commonSetting.DownloadFtp;
                this.txtEncryptDmraFileFolder.Text = commonSetting.FolderForEncryptDmraFile;
                this.txtEncryptPHouseFileFolder.Text = commonSetting.FolderForEncryptPrintingHouseFile;
                this.txtFtpPassword.Text = commonSetting.FtpPassword;
                this.txtFtpUsername.Text = commonSetting.FtpUserName;
                this.txtGenerateDmraFileFolder.Text = commonSetting.FolderForGenerateDmraFile;
                this.txtGeneratePHouseFolder.Text = commonSetting.FolderForGeneratePrintingHouseFile;
                this.txtMailPassword.Text = commonSetting.MailPassword;
                this.txtPHouseFileName.Text = commonSetting.PrintingHouseFileName;
                this.txtPHousePassword.Text = commonSetting.PrintingHousePassword;
                this.txtPHouseRecipient.Text = commonSetting.PrintingHouseRecipient;
                this.txtPHouserFtp.Text = commonSetting.PrintingHouseFtp;
                this.txtPHouseUsername.Text = commonSetting.PrintingHouseUserName;
                this.txtPrivateKeyPassword.Text = commonSetting.PrivateKeyPassword;
                this.txtSmtpServer.Text = commonSetting.SmtpServer;
                this.txtSummaryMail.Text = commonSetting.MailAddressForSummaryReport;
                this.txtSysMail.Text = commonSetting.SystemMailAddress;
                this.txtUploadFtp.Text = commonSetting.UploadFtp;
                this.txtForSummaryReport.Text = commonSetting.FolderForSummaryReport;
                this.txtLeadPerAgent.Text = commonSetting.LeadPerAgent.ToString();
                this.txtLockMonth.Text = commonSetting.LockMonth.ToString();
                this.txtMaxStop.Text = commonSetting.MaxStop.ToString();
                this.txtMinStop.Text = commonSetting.MinStop.ToString();
            }
        }

        protected void btnCommit_Click(object sender, EventArgs e)
        {
            SettingManager sm = new SettingManager();
            CommonSetting cs = new CommonSetting();
            cs.MDForAssignBlankZip = this.txtAssignBlankZip.Text;
            cs.FolderForCompressDmraFile = this.txtCompressDmraFileFolder.Text;
            cs.FolderForCompressPrintingHouseFile = this.txtCompressPHouseFileFolder.Text;
            cs.CurrentYear = this.txtCurrentYear.Text;
            cs.FolderForDecompress = this.txtDecompressFolder.Text;
            cs.FolderForDecrypt = this.txtDecryptFolder.Text;
            cs.DMRARecipient = this.txtDMRARecipient.Text;
            cs.FolderForDownload = this.txtDownloadFolder.Text;
            cs.DownloadFtp = this.txtDownloadFtp.Text;
            cs.FolderForEncryptDmraFile = this.txtEncryptDmraFileFolder.Text;
            cs.FolderForEncryptPrintingHouseFile = this.txtEncryptPHouseFileFolder.Text;
            cs.FtpPassword = this.txtFtpPassword.Text;
            cs.FtpUserName = this.txtFtpUsername.Text;
            cs.FolderForGenerateDmraFile = this.txtGenerateDmraFileFolder.Text;
            cs.FolderForGeneratePrintingHouseFile = this.txtGeneratePHouseFolder.Text;
            cs.MailPassword = this.txtMailPassword.Text;
            cs.PrintingHouseFileName = this.txtPHouseFileName.Text;
            cs.PrintingHousePassword = this.txtPHousePassword.Text;
            cs.PrintingHouseRecipient = this.txtPHouseRecipient.Text;
            cs.PrintingHouseFtp = this.txtPHouserFtp.Text;
            cs.PrintingHouseUserName = this.txtPHouseUsername.Text;
            cs.PrivateKeyPassword = this.txtPrivateKeyPassword.Text;
            cs.SmtpServer = this.txtSmtpServer.Text;
            cs.MailAddressForSummaryReport = this.txtSummaryMail.Text;
            cs.SystemMailAddress = this.txtSysMail.Text;
            cs.UploadFtp = this.txtUploadFtp.Text;
            cs.FolderForSummaryReport = this.txtForSummaryReport.Text;
            cs.LeadPerAgent = int.Parse(this.txtLeadPerAgent.Text);
            cs.LockMonth = int.Parse(this.txtLockMonth.Text);
            cs.MaxStop = int.Parse(this.txtMaxStop.Text);
            cs.MinStop = int.Parse(this.txtMinStop.Text);

            CommonSetting oldSetting = sm.GetCommonSetting();

            if (sm.UpdateCommonSetting(cs))
            {
                SystemConfiguration.CommonSetting = sm.GetCommonSetting();

                Thread thread = new Thread(SendMail);
                thread.Start(oldSetting);

                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), 
                    "updated", "<script type='text/javascript'>alert('You have saved settings successfully! ');</script>");
            }
            else
            {
                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), 
                    "failed", "<script type='text/javascript'>alert('You have failed ! ');</script>");
            }
            BindData();
        }

        private void SendMail(object setting)
        {
            CommonSetting commonSetting = (CommonSetting)setting;
            string changeContent = GetChangeSettings(commonSetting);
            if (changeContent != string.Empty)
            {
                Mail eMail = new Mail();
                eMail.Subject = Constants.MailSubjecttSettingChange;
                eMail.Content = string.Format(Constants.MailContentSettingChange, User.Identity.Name, changeContent, DateTime.Now.Date.ToString("MM/dd/yyyy") + "@" + DateTime.Now.ToShortTimeString());
                AccountManager am = new AccountManager();
                eMail.Recipients = am.GetMailAddress(UserType.Administrator);
                SmtpMailService smtp = new SmtpMailService(SystemConfiguration.CommonSetting.SmtpServer,
                    SystemConfiguration.CommonSetting.SystemMailAddress, SystemConfiguration.CommonSetting.MailPassword);
                smtp.Send(eMail);
            }
        }
        private string GetChangeSettings(CommonSetting commonSetting)
        {
            string changes = string.Empty;
            changes += (commonSetting.MDForAssignBlankZip == SystemConfiguration.CommonSetting.MDForAssignBlankZip ? string.Empty : "MD For Assign Blank Zip,");
            changes += (commonSetting.FolderForCompressDmraFile == SystemConfiguration.CommonSetting.FolderForCompressDmraFile ? string.Empty : "Folder For Compress DMRA File,");
            changes += (commonSetting.FolderForCompressPrintingHouseFile == SystemConfiguration.CommonSetting.FolderForCompressPrintingHouseFile ? string.Empty : "Folder For Compress Printing HouseFile,");
            changes += (commonSetting.CurrentYear == SystemConfiguration.CommonSetting.CurrentYear ? string.Empty : "Current Year,");
            changes += (commonSetting.FolderForDecompress == SystemConfiguration.CommonSetting.FolderForDecompress ? string.Empty : "Folder For Decompress,");
            changes += (commonSetting.FolderForDecrypt == SystemConfiguration.CommonSetting.FolderForDecrypt ? string.Empty : "Folder For Decrypt,");
            changes += (commonSetting.DMRARecipient == SystemConfiguration.CommonSetting.DMRARecipient ? string.Empty : "DMRA Recipient,");
            changes += (commonSetting.FolderForDownload == SystemConfiguration.CommonSetting.FolderForDownload ? string.Empty : "Folder For Download,");
            changes += (commonSetting.DownloadFtp == SystemConfiguration.CommonSetting.DownloadFtp ? string.Empty : "Download Ftp,");
            changes += (commonSetting.FolderForEncryptDmraFile == SystemConfiguration.CommonSetting.FolderForEncryptDmraFile ? string.Empty : "Folder For Encrypt DMRA File,");
            changes += (commonSetting.FolderForEncryptPrintingHouseFile == SystemConfiguration.CommonSetting.FolderForEncryptPrintingHouseFile ? string.Empty : "Folder For Encrypt Printing House File,");
            changes += (commonSetting.FtpPassword == SystemConfiguration.CommonSetting.FtpPassword ? string.Empty : "Ftp Password,");
            changes += (commonSetting.FtpUserName == SystemConfiguration.CommonSetting.FtpUserName ? string.Empty : "Ftp UserName,");
            changes += (commonSetting.FolderForGenerateDmraFile == SystemConfiguration.CommonSetting.FolderForGenerateDmraFile ? string.Empty : "Folder For Generate Dmra File,");
            changes += (commonSetting.FolderForGeneratePrintingHouseFile == SystemConfiguration.CommonSetting.FolderForGeneratePrintingHouseFile ? string.Empty : "Folder For Generate Printing House File,");
            changes += (commonSetting.MailPassword == SystemConfiguration.CommonSetting.MailPassword ? string.Empty : "Mail Password,");
            changes += (commonSetting.PrintingHouseFileName == SystemConfiguration.CommonSetting.PrintingHouseFileName ? string.Empty : "Printing House FileName,");
            changes += (commonSetting.PrintingHousePassword == SystemConfiguration.CommonSetting.PrintingHousePassword ? string.Empty : "Printing House Password,");
            changes += (commonSetting.PrintingHouseRecipient == SystemConfiguration.CommonSetting.PrintingHouseRecipient ? string.Empty : "Printing House Recipient,");
            changes += (commonSetting.PrintingHouseFtp == SystemConfiguration.CommonSetting.PrintingHouseFtp ? string.Empty : "Printing House Ftp,");
            changes += (commonSetting.PrintingHouseUserName == SystemConfiguration.CommonSetting.PrintingHouseUserName ? string.Empty : "Printing House UserName,");
            changes += (commonSetting.PrivateKeyPassword == SystemConfiguration.CommonSetting.PrivateKeyPassword ? string.Empty : "Private Key Password,");
            changes += (commonSetting.SmtpServer == SystemConfiguration.CommonSetting.SmtpServer ? string.Empty : "Smtp Server,");
            changes += (commonSetting.MailAddressForSummaryReport == SystemConfiguration.CommonSetting.MailAddressForSummaryReport ? string.Empty : "Mail Address For Summary Repor,");
            changes += (commonSetting.SystemMailAddress == SystemConfiguration.CommonSetting.SystemMailAddress ? string.Empty : "System Mail Address,");
            changes += (commonSetting.UploadFtp == SystemConfiguration.CommonSetting.UploadFtp ? string.Empty : "Upload Ftp,");
            changes += (commonSetting.FolderForSummaryReport == SystemConfiguration.CommonSetting.FolderForSummaryReport ? string.Empty : "Folder For SummaryReport,");

            if (changes.LastIndexOf(',') != -1)
            {
                changes = changes.Remove(changes.LastIndexOf(','), 1);
            }

            return changes;
        }
    }
}
